package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class ESManagerController {

    @Autowired
    private ESManagerService esManagerService;

    /**
     *添加博客
     * @param uid
     * @return
     */
    @PostMapping("/addElasticSearchIndexByUid")
    public Map<String,Object> addElasticSearchIndexByUid(@RequestParam("uid") String uid){
        esManagerService.addElasticSearchIndexByUid(uid);
        //封装返回数据
        Map result=new HashMap();
        result.put("code","SUCCESS");
        result.put("data","操作成功");

        return result;
    }

    /**
     *通过uid删除博客
     * @param uid
     * @return
     */
    @PostMapping("/deleteElasticSearchByUid")
    public Map<String,Object> deleteElasticSearchByUid(@RequestParam("uid") String uid){
        esManagerService.deleteElasticSearchByUid(uid);
        //封装返回数据
        Map result=new HashMap();
        result.put("code","SUCCESS");
        result.put("data","操作成功");

        return result;
    }

    /**
     *通过uids删除ElasticSearch博客索引
     * @param uids
     * @return
     */
    @PostMapping("/deleteElasticSearchByUids")
    public Map<String,Object> deleteElasticSearchByUids(@RequestParam("uids") String uids){
        String[] split = uids.split(",");
        for (int i = 0; i < split.length; i++) {
            esManagerService.deleteElasticSearchByUid(split[i]);
        }
        //封装返回数据
        Map result=new HashMap();
        result.put("code","SUCCESS");
        result.put("data","操作成功");

        return result;
    }

    /**
     *通过ElasticSearch搜索博客
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/elasticSearchBlog")
    public Map<String,Object> elasticSearchBlog(@RequestParam("keywords") String keywords, @RequestParam(name = "currentPage",defaultValue = "1",required = false) Integer currentPage, @RequestParam(name = "pageSize",defaultValue = "10",required = false) Integer pageSize){
        PageInfo pageInfo = esManagerService.elasticSearchBlog(keywords, currentPage, pageSize);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("totalPage",pageInfo.getPages());
        data.put("total",pageInfo.getTotal());
        data.put("pageSize",pageInfo.getSize());
        data.put("currentPage",pageInfo.getPageNum());

        //封装返回数据
        List list = pageInfo.getList();
        List<Map> blogMap=new ArrayList<>();
        for (Object o : list) {
            Map map = JSON.parseObject(JSON.toJSONString(o), Map.class);
            map.put("all",(String)map.get("title")+" "+(String) map.get("summary"));
            blogMap.add(map);
        }
        data.put("blogList",blogMap);


        //将将data放入返回map
        result.put("data",data);

        return result;
    }

    /**
     *ElasticSearch初始化索引
     * @return
     */
    @PostMapping("/initElasticSearchIndex")
    public Map<String,Object> initElasticSearchIndex(){
        //创建索引库结构
        esManagerService.createMappingAndIndex();
        //导入全部数据
        esManagerService.importAll();
        //封装返回数据
        Map result=new HashMap();
        result.put("code","SUCCESS");
        result.put("data","操作成功");

        return result;
    }
}
